//package com.shop.clientorder.consumer;
//
//import com.shop.car.domain.ShopCarDetails;
//import com.shop.clientorder.domain.FullAdders;
//import com.shop.clientorder.domain.OrderItemVo;
//import com.shop.clientorder.feign.CouponFeign;
//import com.shop.clientorder.feign.ProductRemote;
//import com.shop.clientorder.utils.DelayedQueueUtil;
//import com.shop.common.core.domain.Result;
//import com.shop.common.core.utils.uuid.UUID;
//import com.shop.common.redis.service.RedisService;
//import com.shop.clientorder.domain.OmsOrder;
//import com.shop.clientorder.domain.OmsOrderItem;
//import com.shop.clientorder.feign.ShopCarRemote;
//import com.shop.clientorder.service.IOmsOrderItemService;
//import com.shop.clientorder.service.IOmsOrderService;
//import com.shop.common.security.utils.SecurityUtils;
//import com.shop.product.domain.PmsSkuInfo;
//import lombok.extern.log4j.Log4j2;
//import org.apache.kafka.clients.consumer.ConsumerRecord;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.kafka.annotation.KafkaListener;
//import org.springframework.kafka.support.Acknowledgment;
//import org.springframework.stereotype.Component;
//import com.shop.clientorder.config.MyKafkaListenerErrorHandler;
//
//import java.math.BigDecimal;
//import java.util.ArrayList;
//import java.util.List;
//
//
///**
// * @Author：zjd
// * @Package：com.bwie.list.consumer
// * @Project：demo
// * @name：Consumer
// * @Date：2024/6/16 19:24
// */
//@Log4j2
//@Component
//public class KafkaConsumer {
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//    @Autowired
//    private RedisService redisService;
//    @Autowired
//    private IOmsOrderService omsOrderService;
//    @Autowired
//    private IOmsOrderItemService omsOrderItemService;
//    @Autowired
//    private ShopCarRemote shopCarRemote;
//    @Autowired
//    private ProductRemote productRemote;
//    @Autowired
//    private DelayedQueueUtil delayedQueueUtil;
//    @Autowired
//    private CouponFeign couponFeign;
//
//    @KafkaListener(topics = "SEND_ADD_ORDER",
//            containerFactory = "kafkaListenerContainerFactory",
//            errorHandler = "myKafkaListenerErrorHandler")
//    public void Mq(ConsumerRecord<String, Object> consumerRecord, Acknowledgment acknowledgment){
//        String key = consumerRecord.key().toString();
//        OrderItemVo orderItemVo = (OrderItemVo) consumerRecord.value();
//        System.out.println("key为：{}，value为：{}"+key+orderItemVo);
//        String s = UUID.randomUUID().toString();
//        System.out.println("入参为"+orderItemVo);
//        //封装了一个添加订单的方法
//        OmsOrder omsOrder = orderAdd(orderItemVo);
//        omsOrderService.addOrder(omsOrder);
//        //延迟队列判断是否支付成功
//        delayedQueueUtil.sendDelayedQueue("PAYMENTS",omsOrder,15*60*1000);
//        //封装添加订单详情的方法
//        OmsOrderItem omsOrderItem = orderItemAdd(orderItemVo, omsOrder);
//        omsOrderItemService.addOrderItem(omsOrderItem);
//        System.out.println("添加的订单为"+omsOrder);
//        System.out.println("添加的订单详情为"+omsOrderItem);
//
//        acknowledgment.acknowledge();
//        System.out.println("消费成功！！！");
//    }
//
//    private OmsOrder orderAdd(OrderItemVo orderItemVo) {
//        ArrayList<ShopCarDetails> list = new ArrayList<>();
//        BigDecimal sum = null;
//        BigDecimal Percentage=new BigDecimal("0.0001");
//        for (String detailsId : orderItemVo.getDetailsIds()) {
//            Result<ShopCarDetails>data = shopCarRemote.findShopCarDetails(Long.valueOf(detailsId));
//            ShopCarDetails shopCarDetails = data.getData();
//            list.add(shopCarDetails);
//        }
//        for (ShopCarDetails shopCarDetails : list) {
//            sum=sum.add(shopCarDetails.getDetailsTotalMoney());
//        }
//        Long userId = SecurityUtils.getUserId();
//        FullAdders fullAdders=omsOrderItemService.FindAddrByUserId(userId);
//        OmsOrder omsOrder = new OmsOrder();
//        omsOrder.setOrderSn(orderItemVo.getOrderSn());
//        omsOrder.setMemberUsername(fullAdders.getFullAddersUserName());
//        omsOrder.setReceiverProvince(fullAdders.getProvinceName());
//        omsOrder.setReceiverCity(fullAdders.getCityName());
//        omsOrder.setReceiverRegion(fullAdders.getRegionName());
//        omsOrder.setPayAmount(orderItemVo.getPayMoney());
//        omsOrder.setTotalAmount(sum);
//        omsOrder.setFreightAmount(sum.multiply(Percentage));
//        omsOrder.setCouponAmount(orderItemVo.getCouponAmount());
//        omsOrder.setPayType(orderItemVo.getPayType());
//        omsOrder.setCouponId(orderItemVo.getCouponId());
//        omsOrder.setStatus(0);
//
//        return omsOrder;
//    }
//
//    private OmsOrderItem orderItemAdd(OrderItemVo orderItemVo, OmsOrder omsOrder) {
//        List<String> skuIds = orderItemVo.getSkuIds();
//        OmsOrderItem omsOrderItem = new OmsOrderItem();
//        for (String skuId : skuIds) {
//            Result<PmsSkuInfo> data = productRemote.getSkuInfo(Long.valueOf(skuId));
//            PmsSkuInfo pmsSkuInfo = data.getData();
//            Long orderId=omsOrderService.findOrderIdByOrderSn(orderItemVo.getOrderSn());
//            omsOrderItem.setOrderId(orderId);
//            omsOrderItem.setOrderSn(orderItemVo.getOrderSn());
//            omsOrderItem.setSkuId(pmsSkuInfo.getSkuId());
//            omsOrderItem.setSkuName(pmsSkuInfo.getSkuName());
//            omsOrderItem.setSkuPic(pmsSkuInfo.getSkuDefaultImg());
//            omsOrderItem.setSkuPrice(pmsSkuInfo.getPrice());
//            omsOrderItem.setSkuQuantity(orderItemVo.getTotalCount());
//        }
//
//        return omsOrderItem;
//    }
//
//}
